import axios from "axios";
import {Base64} from "js-base64";
export default {

    async decode(searchStatus, origin, keyword, author_org, condFirst, condList) {
        // throw new Error("API demo error")
        // eslint-disable-next-line no-unreachable
         var data = {
            "type": "papers",
            "must": [],
            "must_not": [],
            "should": [],
            "offset": searchStatus.offset,
            "size": 20,
             "condFirst": condFirst,
             "condList" : condList,
             "searchStatus" : searchStatus,
             "origin" : origin,
             "keyword" : keyword,
             "author_org" : author_org,
             "tab" : 0,
        };
        let type = searchStatus.searchType;
        if(type === 1) {
            data.sort = {
                "year": "desc",
            }
        }
        else if(type === 2) {
            data.sort = {
                "n_citation": "desc",
            }
        }

        if(origin.flag) {
            data.range = {
                "gte" : origin.gte,
                "lte" : origin.lte,
            }
        }


        if(keyword.flag && keyword.keyword) {
            data.must.push({
                "keywords": keyword.keyword,
            })

        }

        // console.log(author_org)

        if(author_org.flag && author_org.org_id) {
            data.must.push({
                "authors.org": author_org.org_id,
            })
        }

        if(condFirst.content) {
            if(condFirst.key === "标题" ) {
                data.must.push({
                    "title": condFirst.content,
                })
            }
            else {
                data.must.push({
                    "all": condFirst.content,
                })
            }
        }

        for (let i = 0; i < condList.list.length; i ++) {
            let tmp = condList.list[i];
            if(!tmp[2]) continue;
            var q;
            if(tmp[1] === "作者") {
                q = {
                    "authors.name": tmp[2],
                }
            }
            else if(tmp[1] === "出版物标题") {
                q = {
                    "venue.name": tmp[2],
                }
            }
            else {
                q = {
                    "keywords": tmp[2],
                }
            }
            if(tmp[0] === "AND") {
                data.must.push(q);
            }
            else if(tmp[0] === "OR") {
                data.should.push(q);
            }
            else {
                data.must_not.push(q);
            }
        }

        // console.log(data)
        var str = JSON.stringify(data)
        // console.log(str)
        str = Base64.encode(str);
        // console.log(str)

        // str = Base64.decode(str)
        // console.log(str)

        return {
            str: str,
        }
    },

    async get(str, isMounted) {
        var res;
        var data = JSON.parse(Base64.decode(str))
        var query = {

        };
        query.must = data.must
        query.must_not = data.must_not
        query.should = data.should
        query.offset = data.offset
        query.size = data.size
        query.type = data.type
        if(data.sort) query.sort = data.sort
        if(data.range) query.range = data.range

        console.log(data.author_org, data.keyword)

        var keyValues;
        var flag = !isMounted && (data.keyword.flag || data.author_org.flag)
        if(!flag) {
            await  axios.post('/api/scholar/papers/pseudo_aggs_search/', query,
                {headers: {'Content-Type': 'application/json; charset=utf-8'}}
            ).then(function (response) {
                keyValues = response
                // console.log(keyValues);
            })
        }


        await axios.post('/api/scholar/papers/pseudo_search/', query,
            {headers: {'Content-Type': 'application/json; charset=utf-8'}}
        ).then(function (response) {
            res = response
            // console.log(response);
        })

        // console.log(res)

        return {
            data: res.data.data,
            aggs: flag? null : keyValues.data.aggs,
            paperList: res.data.data.data.map(x=> {
                return {
                    id: x.id,
                    title: x.title,
                    authors: x.authors,
                    venue: x.venue ? x.venue : '',
                    doi: x.doi?x.doi:'',
                    issue: x.issue?x.issue:'',
                    lang:x.lang?x.lang:'',
                    n_citation: x.n_citation ? x.n_citation : 0,
                    pages_start: x.pages_start ? x.pages_start : '',
                    pages_end: x.pages_end ? x.pages_end : '',
                    abstract: x.abstract ? x.abstract: '',
                    url:x.url?x.url:'',
                    volume:x.volume?x.volume:'',
                    keywords:x.keywords?x.keywords:'',
                    year: x.year ? x.year : '',
                    pdf: x.pdf?x.pdf:'',
                }
            }),
        }
    },
}